*------------------------------------------------------------------------------*
						*** REPLICATION PACKAGE ***
*------------------------------------------------------------------------------*

clear all
capture log close
capture program drop _all
version 15.1
set varabbrev on
program drop _all
set more off
capture macro drop _all
pause on
set matsize 800
*set matsize 5000
set seed 1234567890
set graphics off
set linesize 255


*------------------------------------------------------------------------------*

*** MACROS AND PROGRAMS ***

**Macros

*Main directory
global path "C:/Users/ste_t/OneDrive/Desktop/pollutionvoting/replication_pack/" // path on Stefano's personal computer
*global path "/Users/ben_elsner/github repos/pollutionvoting/replication_pack/"  // path on Ben's computer

*Subfolders
global rawdata 			"${path}rawdata/"
global dofiles 			"${path}codes/"
global tempdata 		"${path}tempdata/"
global outputs 			"${path}outputs/"

*Translate unicode YES or NO(should only be done the first time the dataset is built)
global unicode=0


*Regression options
global absorb_opt "absorb(kreis_code election_date)"
global vce_opt "vce(cluster kreis_code)"
global hdfe_opt "${absorb_opt} ${vce_opt}" 

*Formats for table entries
global f_b "%5.4f"
global f_se "%4.3f"
global f_YM "%3.2f"
global f_N "%6.0f"
global f_r2 "%4.3f"
global f_F "%4.2f"
global f_LM "%5.2f"
global f_mu "%10.2fc"
global f_mu_b "%10.0fc"
global f_sdt "%10.2fc"
global f_sdt_b "%10.0fc"
global f_sdw "%10.2fc"
global f_sdw_b "%10.0fc"
global f_min "%10.2fc"
global f_min_b "%10.0fc"
global f_max "%10.2fc"
global f_max_b "%10.0fc"
global f_Nc "%6.0fc"
global f_b_bt "%10.3f"
global f_YM_bt "%10.2f"

		
*****************************************

**Programs

*Translate unicode for stata codes (default is 0)
/*
Note: this should only be run the first time you build the dataset.
If you run it more times, it may undo some of the changes
*/
if ${unicode} == 1 {
	cap shell rmdir "${dir_dataclean}/bak.stunicode" /s 
	cd "${dir_dataclean}"
	unicode encoding set "ISO-8859-1"
	unicode translate *.do
}

*Store all numbers reported in the manuscript that are generated by the code in a .txt file
file open myfile using "${outputs}in_text_numbers_generated_in_stata.txt", write text replace
file write myfile "THESE ARE ALL THE NUMBERS REPORTED IN THE MANUSCRIPT THAT ARE GENERATED BY THE CODE IN STATA" _n(1)
file close myfile



*------------------------------------------------------------------------------*

*** RUN ***

local do_data	= 1
local do_merge 	= 1
local do_descr_stat = 1
local do_regressions = 1


*------------------------------------------------------------------------------*

*** CALL DO-FILES ***

**Inverse distance program
qui do "${dofiles}distanceprog.do"

**Data
if `do_data'	== 1 			do "${dofiles}1_wind_iv.do"									// Prepares wind direction IVs
if `do_data'	== 1 			do "${dofiles}1_reformed_counties_votes.do" 				// Prepares voting dataset for reformed counties pre-reform
if `do_data'	== 1 			do "${dofiles}1_reformed_counties_pop.do" 					// Prepares population dataset for reformed counties pre-reform
if `do_data'	== 1 			do "${dofiles}1_data.do" 									// Prepares voting, pollution, weather and socio-economic datasets
if `do_data'	== 1 			do "${dofiles}1_centroids.do" 								// Produces maps
if `do_merge'	== 1 			do "${dofiles}2_merge.do" 									// Merges municipalities with stations
if `do_data'	== 1 			do "${dofiles}2_estimation_sample.do"  						// Generates estimation sample
if `do_data'	== 1 			do "${dofiles}1_data_poll_weather_eldate_plusminus30d.do"  	// Creates dataset with county-level pollution and weather in the 30 days before/after each election
/*This creates the SOEP dataset used to produce Figure 2. It is based on confidential data that we are not allowed to share
if `do_data'	== 1 			do "${dofiles}1_SOEP_merge.do"								// Merges SOEP data to pollution and weather values
if `do_data'	== 1 			do "${dofiles}1_SOEP_reg.do"								// Performs regression on SOEP data used for Figure 2
*/

**Tables and figures
*Main paper
if `do_regressions' == 1		do "${dofiles}3_table1.do" 									// Contemporaneous effects of PM10 in 10s of micrograms
/* This is based on confidential data that we are not allowed to share
if `do_regressions' == 1		do "${dofiles}3_figure1.do"									// Effect of PM10 on voting intentions and government approval (Politbarometer)
if `do_regressions' == 1		do "${dofiles}3_figure2.do"									// Effect of PM10 on political preferences and affective well-being (SOEP)
*/

*Supplementary Information
if `do_descr_stat' 	== 1			do "${dofiles}4_figure_S1a.do"  						// Distribution of PM10 in election days plus reference values
if `do_descr_stat' 	== 1			do "${dofiles}4_figure_S1b.do"  						// Box-Plot of PM10 for deciles of population
if `do_descr_stat' == 1				do "${dofiles}4_figure_S2.do"  							// Changes in incumbent vote share in federal elections
if `do_descr_stat' 	== 1			do "${dofiles}4_table_S1.do" 							// Summary statistics
if `do_descr_stat' == 1				do "${dofiles}4_figure_S3.do"  							// Frequency of incumbency and opposition by party and election type
if `do_descr_stat' 	== 1			do "${dofiles}4_figure_S4.do"  							// Numner of elections by calendar month
if `do_regressions' == 1			do "${dofiles}4_figure_S5.do" 							// Dose-response relationship: PM10 on vote share for incumbent parties
if `do_regressions' == 1			do "${dofiles}4_table_S4.do" 							// Robustness checks: IV, temperature dummies, NO2, asymmetric effects, other voting outcomes
if `do_regressions' == 1			do "${dofiles}4_figure_S6.do"  							// Effect of PM10 on voting with placebo election dates
if `do_regressions' == 1			do "${dofiles}4_figure_S7.do"  							// Permutation tests
if `do_regressions' == 1			do "${dofiles}4_table_S5.do"  							// Regression of lagged PM10 on contemporaneous PM10 (attenuation bias)
if `do_descr_stat' == 1				do "${dofiles}4_figure_S8.do"  							// Trends in average PM10 by year
if `do_descr_stat' == 1				do "${dofiles}4_figure_S9.do"  							// Trends in average PM10 by election date
if `do_descr_stat' == 1				do "${dofiles}4_figure_S10.do"  						// Boxplot of concentration of PM10 over week days
if `do_descr_stat' == 1				do "${dofiles}4_figure_S11a.do"  						// PM10 in Munich: Sundays 2016
if `do_descr_stat' == 1				do "${dofiles}4_figure_S11b.do"  						// PM10 in Munich: election dates
if `do_descr_stat' == 1				do "${dofiles}4_figure_S12a.do"  						// Histograms of residulas of PM10: without controls
if `do_descr_stat' == 1				do "${dofiles}4_figure_S12b.do"  						// Histograms of residulas of PM10: with controls
if `do_descr_stat' == 1				do "${dofiles}4_figure_S13.do"  						// Histogram of within-county sd
if `do_regressions' == 1			do "${dofiles}4_table_S6.do"  							// Robustness check removing Mecklenburg-Vorpommern, Sachsen and Sachsen-Anhalt
if `do_regressions' == 1			do "${dofiles}4_table_S7.do"  							// Contemporaneous effect of PM10 using differen radiuses
if `do_regressions' == 1			do "${dofiles}4_table_S8.do"  							// Effect of PM10 on socio-economic characteristics (balance tests)
if `do_descr_stat' == 1				do "${dofiles}4_figure_S14S15data.do"  					// Data preparation for dumbbell graphs S14 and S15
if `do_descr_stat' == 1				do "${dofiles}4_figure_S15.do"  						// Dumbbell Graph S15: difference in predicted vote shares
/* 
To produce figure S14, please run the R script 4_figure_S14.R; see README for instructions
*/


*------------------------------------------------------------------------------*

exit
